from pydantic import model_validator
from app.schema.conservation_schema import *
from app.schema.agent_schema import *
from app.schema.knowledge_base_schema import *
from app.schema.plugin_schema import *
from app.schema.embedding_schema import *
from app.schema.collection_schema import *
from app.schema.graph_collection_schema import *
from app.schema.text_block_schema import *
from app.schema.llm_provider_schema import *


class GetAgentDetail(AgentDetailSchema):
    conservations: list[ConservationListSchema] | None = '[]'  # 与智能体相关联的会话
    plugins: list[PluginDetailSchema] | None = []  # 与智能体相关联的插件
    knowledge_bases: list[KnowledgeBaseListSchema] | None = None  # 与智能体相关联的知识库

    @model_validator(mode='after')
    def handle(self):
        sample_query = self.sample_query
        if sample_query is not None:
            self.sample_query = json.loads(sample_query)

        spl_form = self.spl_form
        if spl_form is not None:
            self.spl_form = json.loads(spl_form)

        spl_chain = self.spl_chain
        if spl_chain is not None:
            self.spl_chain = json.loads(spl_chain)

        parameters = self.parameters
        if parameters is not None:
            self.parameters = json.loads(parameters)

        conservations = self.conservations
        if conservations is not None:
            print(self.operator_uuid)
            # 过滤出符合条件的会话（即user_uuid匹配的会话）
            self.conservations = [conservation for conservation in conservations if
                                  conservation.user_uuid == self.operator_uuid]
        return self


# 当前 Agent 信息详情
class GetAgentWorkSpace(AgentDetailSchema):
    conservations: list[ConservationDetailSchema] | None = '[]'  # 与智能体相关联的会话
    plugins: list[PluginDetailSchema] | None = None  # 与智能体相关联的插件
    knowledge_bases: list[GetKnowledgeBaseWorkSpace] | None = None  # 与智能体相关联的知识库

    @model_validator(mode='after')
    def handle(self):
        sample_query = self.sample_query
        if sample_query is not None:
            self.sample_query = json.loads(sample_query)

        spl_form = self.spl_form
        if spl_form is not None:
            self.spl_form = json.loads(spl_form)

        spl_chain = self.spl_chain
        if spl_chain is not None:
            self.spl_chain = json.loads(spl_chain)

        parameters = self.parameters
        if parameters is not None:
            self.parameters = json.loads(parameters)

        conservations = self.conservations
        if conservations is not None:
            # 过滤出符合条件的会话（即user_uuid匹配的会话）
            self.conservations = [conservation for conservation in conservations if
                                  conservation.user_uuid == self.user_uuid]
        return self


# 当前 Agent 信息详情
class GetAgentList(AgentListSchema):
    pass


class GetConservationDetail(ConservationDetailSchema):
    agent: AgentListSchema
    knowledge_base: KnowledgeBaseListSchema
    collection: CollectionListSchema


# 当前 Conservation 信息详情
class GetConservationList(ConservationListSchema):
    pass
